package edu.mse.camel.client.core.plugin;

import org.eclipse.ui.IStartup;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import edu.mse.camel.client.core.handlers.NotifyParticipantChangeEventHandler;
import edu.mse.camel.client.core.handlers.NotifySessionRoleChangeEventHandler;
import edu.mse.camel.client.core.handlers.NotifyTeamStatusChangeEventHandler;
import edu.mse.camel.client.eventbus.pubsub.EventManager;
import edu.mse.camel.client.events.EventIDs;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin implements IStartup {

	@Override
	public void earlyStartup() {
		// do nothing
		
	}

	// The plug-in ID
	public static final String PLUGIN_ID = "edu.mse.camel.client.core";

	// The shared instance
	private static Activator plugin;
	
	/**
	 * The constructor
	 */
	public Activator() {
	}

	private final NotifyParticipantChangeEventHandler npceh = 
		new NotifyParticipantChangeEventHandler();
	
	private final NotifySessionRoleChangeEventHandler nsrc = 
		new NotifySessionRoleChangeEventHandler();
	
	private final NotifyTeamStatusChangeEventHandler ntsch = 
		new NotifyTeamStatusChangeEventHandler();
	
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
		EventManager.getInstance().subscribeTo(
				EventIDs.NOTIFY_SESSION_PARTICIPANT_CHANGE_EVENT, npceh);
		EventManager.getInstance().subscribeTo(
				EventIDs.NOTIFY_SESSION_ROLE_CHANGE_EVENT, nsrc);
		EventManager.getInstance().subscribeTo(
				EventIDs.NOTIFY_TEAM_STATUS_CHANGE_EVENT, ntsch);
		
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
		EventManager.getInstance().unSubscribeTo(
				EventIDs.NOTIFY_SESSION_PARTICIPANT_CHANGE_EVENT, npceh);
		EventManager.getInstance().unSubscribeTo(
				EventIDs.NOTIFY_SESSION_ROLE_CHANGE_EVENT, nsrc);
		EventManager.getInstance().unSubscribeTo(
				EventIDs.NOTIFY_TEAM_STATUS_CHANGE_EVENT, ntsch);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}

}
